package com.dongdongqiang.daogenerator.tablemetabuilder.postgresql;

import com.dongdongqiang.daogenerator.tablemetabuilder.EntityToEntityMetaInfoConvertor;
import com.dongdongqiang.daogenerator.annotation.ColumnDes;
import com.dongdongqiang.daogenerator.pojo.TableColumn;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.persistence.Id;
import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Map;

/**
 * Created by ZZB on 2017/12/3.
 */
public class PostgresqlEntityToEntityMetaInfoConvertor implements EntityToEntityMetaInfoConvertor {
    private static final Logger logger= LoggerFactory.getLogger(PostgresqlEntityToEntityMetaInfoConvertor.class);
    @Override
    public void optimizeTableColumn(TableColumn tableColumn, Field field) {
        Class clazz = field.getType();
        ColumnDes columnDes = field.getAnnotation(ColumnDes.class);
        if (clazz.equals(String.class)) {
            if (columnDes == null) {
                tableColumn.setColumnType("TEXT");
            } else {
                if (tableColumn.getMaxLength() == null) {
                    tableColumn.setColumnType("TEXT");
                } else if( tableColumn.getMaxLength() <= 500) {
                    tableColumn.setColumnType("VARCHAR");
                    tableColumn.setMaxLength(tableColumn.getMaxLength());
                } else {
                        tableColumn.setColumnType("TEXT");
                }
            }
        } else if (clazz.equals(Integer.class) || clazz.equals(int.class)) {
            if (columnDes == null || columnDes.maximum().equals("")) {
                tableColumn.setColumnType("INT4");
            } else {
                BigDecimal maximunDecimal = new BigDecimal(columnDes.maximum());
                if (maximunDecimal.compareTo(new BigDecimal(32767)) <= 0) {
                    tableColumn.setColumnType("INT2");
                } else if (maximunDecimal.compareTo(new BigDecimal(32767)) > 0) {
                    tableColumn.setColumnType("INT4");
                }
            }
        } else if (clazz.equals(Long.class) || clazz.equals(long.class)) {
            tableColumn.setColumnType("INT8");
        } else if (clazz.equals(Float.class) || clazz.equals(float.class)) {
            tableColumn.setColumnType("FLOAT4");
        } else if (clazz.equals(Double.class) || clazz.equals(double.class)) {
            tableColumn.setColumnType("FLOAT8");
        } else if (clazz.equals(Boolean.class) || clazz.equals(boolean.class)) {
            tableColumn.setColumnType("BIT");
        } else if (clazz.equals(Boolean[].class) || clazz.equals(boolean[].class)) {
            tableColumn.setColumnType("VARBIT");
            if (columnDes == null) {
                tableColumn.setMaxLength(255L);
            }else {
                tableColumn.setMaxLength(columnDes.maxLength());
            }
        } else if (clazz.equals(Byte[].class) || clazz.equals(byte[].class)) {
            tableColumn.setColumnType("BYTEA");
        } else if (clazz.equals(BigDecimal.class)) {
            tableColumn.setColumnType("NUMERIC");
            if (columnDes == null) {
                tableColumn.setPrecision(20L);
                tableColumn.setScale(4L);
            } else {
                if (columnDes.precision() == 0L) {
                    tableColumn.setPrecision(20L);
                } else {
                    tableColumn.setPrecision(columnDes.precision());
                }
                if (columnDes.scale() == 0L) {
                    tableColumn.setScale(4L);
                } else {
                    tableColumn.setScale(columnDes.scale());
                }
            }
        } else if (clazz.equals(BigInteger.class)) {
            tableColumn.setColumnType("NUMERIC");
            tableColumn.setScale(0L);
            if (columnDes == null) {
                tableColumn.setPrecision(20L);
            } else {
                if (columnDes.precision() == 0L) {
                    tableColumn.setPrecision(20L);
                } else {
                    tableColumn.setPrecision(columnDes.precision());
                }
            }
        } else if (clazz.equals(java.sql.Date.class)){
            tableColumn.setColumnType("DATE");
        } else if (clazz.equals(java.util.Date.class)){
            tableColumn.setColumnType("TIMESTAMP");
        } else if ( clazz.equals(Map.class)){
            tableColumn.setColumnType("JSON");
        } else {
            logger.error("不支持的数据类型！field[{}],type[{}]",field.getName(),clazz.getName());
        }

        if(columnDes != null){
            tableColumn.setDefaultValue(columnDes.defaultValue());
            tableColumn.setNullable(columnDes.isNullable());
        }

        Id idAnn = field.getAnnotation(Id.class);
        if(idAnn != null){
            tableColumn.setPrimaryKey(true);
        }

    }
}
